{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# chap3 列表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "列表让你能够在一个地方存储成组的信息，其中可以只包含几个元素，也可以包含数百个元素。列表是新手可直接使用的最强大的Python功能之一， 它融合了众多重要的编程概念。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.1 列表是什么"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**列表** 由一系列按特定顺序排列的元素组成。鉴于列表通常包含多个元素，给列表指定一个表示复数的名称（如letters, digits）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['trek', 'cannondale', 'redline', 'specialized']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bicycles = ['trek', 'cannondale', 'redline', 'specialized']\n",
    "bicycles"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 3.1.1 访问列表元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'trek'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bicycles[0]  # 使用索引访问元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当然你也可以对任何元素调用字符串方法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Trek'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bicycles[0].title()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**我们需要注意的是索引从0开始的，而不是从1**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第0个元素是trek\n",
      "第1个元素是cannondale\n",
      "第2个元素是redline\n",
      "第3个元素是specialized\n"
     ]
    }
   ],
   "source": [
    "for i in range(len(bicycles)):\n",
    "    print('第'+str(i)+'个元素是'+bicycles[i])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当然还有一些常用的骚套路"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'specialized'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bicycles[-1]  # 访问最后一个元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 动手试一试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jack\n",
      "Bedoom\n",
      "Even\n"
     ]
    }
   ],
   "source": [
    "# 3.1\n",
    "names = ['Jack', 'Bedoom', 'Even']\n",
    "for i in range(len(names)):  # 以后会说\n",
    "    print(names[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jack, hello\n",
      "Bedoom, hello\n",
      "Even, hello\n"
     ]
    }
   ],
   "source": [
    "# 3.2\n",
    "for i in range(len(names)):\n",
    "    print(names[i]+', hello')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2 修改、添加和删除元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 3.2.1 修改列表元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "修改列表元素的语法与访问列表元素的语法有些类似。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['honda', 'yamaha', 'suzuki']\n"
     ]
    }
   ],
   "source": [
    "motorcycles = ['honda', 'yamaha', 'suzuki']\n",
    "print(motorcycles)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['ducati', 'yamaha', 'suzuki']\n"
     ]
    }
   ],
   "source": [
    "motorcycles[0] = 'ducati'  # 直接索引修改\n",
    "print(motorcycles)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 3.2.2 在列表中添加元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. 在列表末尾添加元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['ducati', 'yamaha', 'suzuki', 'honda']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "motorcycles.append('honda')\n",
    "motorcycles"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "append()将元素'honda'添加到列表末尾，而不影响列表中其它元素。你可以灵活地使用它。例如动态地创建列表。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['honda', 'yamaha', 'suzuki']\n"
     ]
    }
   ],
   "source": [
    "motorcycles = []\n",
    "\n",
    "motorcycles.append('honda')\n",
    "motorcycles.append('yamaha')\n",
    "motorcycles.append('suzuki')\n",
    "\n",
    "print(motorcycles)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. 在列表中插入元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用insert()方法，可以在任何地方添加元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['ducati', 'honda', 'yamaha', 'suzuki']\n"
     ]
    }
   ],
   "source": [
    "motorcycles.insert(0, 'ducati')\n",
    "print(motorcycles)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 3.2.3 从列表中删除元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "你经常需要从列表中删除一个或多个元素。你可以根据位置或值来delete列表中的元素。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. 使用del语句删除元素 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果知道要删除的元素在列表中的位置，可使用del语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['honda', 'yamaha', 'suzuki']\n"
     ]
    }
   ],
   "source": [
    "motorcycles = ['honda', 'yamaha', 'suzuki']\n",
    "print(motorcycles)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "del motorcycles[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['yamaha', 'suzuki']\n"
     ]
    }
   ],
   "source": [
    "print(motorcycles)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "因此我们删除了列表中的第0个'honda'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "不仅如此，你也可以使用del删除列表motorcycles"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'motorcycles' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-18-9a26192a10da>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mdel\u001b[0m \u001b[0mmotorcycles\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mmotorcycles\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'motorcycles' is not defined"
     ]
    }
   ],
   "source": [
    "del motorcycles\n",
    "motorcycles  # 会报错的哦"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. 使用方法pop()删除元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有时候，你要将元素删除，并且接着使用它的值。方法pop()可删除列表末尾的元素，并让你能够接着使用它。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The last motorcycle I owned was a Suzeki.\n"
     ]
    }
   ],
   "source": [
    "motorcycles = ['honda', 'yamaha', 'suzeki']\n",
    "\n",
    "last_owned = motorcycles.pop()\n",
    "print('The last motorcycle I owned was a ' + last_owned.title() + '.')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "实际上，你可以使用pop()来删除列表中任何位置的元素，只需在括号中指定要删除的元素的索引即可"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The first motorcycle I owned was a Honda.\n"
     ]
    }
   ],
   "source": [
    "motorcycles = ['honda', 'yamaha', 'suzuki']\n",
    "first_owned = motorcycles.pop(0)\n",
    "print('The first motorcycle I owned was a ' + first_owned.title() + '.')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. 根据值删除元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有时候，你不知道要从列表中删除的值所处的位置。如果你只知道要删除的元素的值，可使用方法remove()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['honda', 'yamaha', 'suzuki', 'ducati']\n"
     ]
    }
   ],
   "source": [
    "motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']\n",
    "print(motorcycles)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['honda', 'yamaha', 'suzuki']\n"
     ]
    }
   ],
   "source": [
    "motorcycles.remove('ducati')\n",
    "print(motorcycles)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**注意：** 方法remove()只删除第一个指定的值。如果要删除的值可能在列表中出现多次，就需要使用循环判断是否删除所有这样的值。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.3 组织列表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 3.3.1 使用sort()对列表进行永久性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['audi', 'bmw', 'subaru', 'toyota']\n"
     ]
    }
   ],
   "source": [
    "cars = ['bmw', 'audi', 'toyota', 'subaru']\n",
    "cars.sort()\n",
    "print(cars)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "相反顺序列表元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['toyota', 'subaru', 'bmw', 'audi']\n"
     ]
    }
   ],
   "source": [
    "cars.sort(reverse=True)\n",
    "print(cars)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 3.3.2 使用函数sorted()对列表进行临时排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Here is the original list:\n",
      "['bmw', 'audi', 'toyota', 'subaru']\n"
     ]
    }
   ],
   "source": [
    "cars = ['bmw', 'audi', 'toyota', 'subaru']\n",
    "print(\"Here is the original list:\")\n",
    "print(cars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Here is the sorted list:\n",
      "['audi', 'bmw', 'subaru', 'toyota']\n"
     ]
    }
   ],
   "source": [
    "print(\"\\nHere is the sorted list:\")\n",
    "print(sorted(cars))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Here is the original list again:\n",
      "['bmw', 'audi', 'toyota', 'subaru']\n"
     ]
    }
   ],
   "source": [
    "print(\"\\nHere is the original list again:\")\n",
    "print(cars)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 3.3.3 倒着打印列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['subaru', 'toyota', 'audi', 'bmw']\n"
     ]
    }
   ],
   "source": [
    "cars.reverse()\n",
    "print(cars)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 3.3.4 确定列表的长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(cars)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.4 使用列表时避免索引错误"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* n个元素的索引是0 1 2 ... n-1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 列表为空时不可能被访问"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.5 小结"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在本章中，你学习了：\n",
    "1. 列表时什么以及如何使用其中的元素；\n",
    "2. 如何定义列表以及如何增删元素；\n",
    "3. 如何对列表进行永久性排序，以及如何为展示列表而进行临时排序；\n",
    "4. 如何确定列表的长度;\n",
    "5. 在使用列表时如何避免索引错误。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# chap4 列表操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当我们要遍历整个列表时，通常是使用for语句。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "alice\n",
      "david\n",
      "carolina\n"
     ]
    }
   ],
   "source": [
    "magicians = ['alice', 'david', 'carolina']\n",
    "for magician in magicians:\n",
    "    print(magician)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "magicians是一个列表，而magician就是在这列表中顺序地取出一个值，赋值于它。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "例如，上行代码中Python解释器首先赋值magician = 'alice'， 然后打印输出；又赋值magician = 'david'， 打印输出；再赋值magician = 'carolina'，打印输出。因为没有元素需要赋值了，所有就结束了。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**一般命名规则就是使用单数来命名临时变量**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dogs = ['Cake', 'Shit', 'David']\n",
    "cars = ['Frist', 'Robot', 'People']\n",
    "for dog in dogs:\n",
    "    print(dog)\n",
    "for car in cars:\n",
    "    print(car)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当然了， 你可以在for循环中做很多事情。这就像搭积木一样。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Alicethat was a great trick\n",
      "Davidthat was a great trick\n",
      "Carolinathat was a great trick\n"
     ]
    }
   ],
   "source": [
    "magicians = ['alice', 'david', 'carolina']\n",
    "\n",
    "for magician in magicians:\n",
    "    print(magician.title() + 'that was a great trick')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "python 是有强制缩进的语言，不可随意使用空格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "IndentationError",
     "evalue": "expected an indented block (<ipython-input-4-45fba15e06eb>, line 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-4-45fba15e06eb>\"\u001b[1;36m, line \u001b[1;32m3\u001b[0m\n\u001b[1;33m    print(magician.title() + 'that was a great trick')\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m expected an indented block\n"
     ]
    }
   ],
   "source": [
    "magicians = ['alice', 'david', 'carolina']\n",
    "\n",
    "for magician in magicians:\n",
    "print(magician.title() + 'that was a great trick')  # 这样就会报错"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "那该如何跳出循环呢？其实很简单，上面提到Python是一个有着强制缩进的语言，我只要更改缩进就可以了。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Alicethat was a great trick\n",
      "Davidthat was a great trick\n",
      "Carolinathat was a great trick\n",
      "Thank you for the wonderful show\n"
     ]
    }
   ],
   "source": [
    "magicians = ['alice', 'david', 'carolina']\n",
    "\n",
    "for magician in magicians:\n",
    "    print(magician.title() + 'that was a great trick')\n",
    "print('Thank you for the wonderful show')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**所以要注意缩进哦**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.2 创建数字列表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 4.2.1 range()函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果你需要一串有规律的数字可以使用range函数。例如你创建一个0 1 2 3 4 5 6 7 8 9的数字列表，我不用ls = [0 1 2 3 4 5 6 7 8 9]这么写了，只需"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ls = list(range(10))  # 如果不加强制类型转换，会变成什么样呢？\n",
    "ls"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "他还可以指定步长"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 2, 4, 6, 8]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 打印0-10的偶数\n",
    "ls = list(range(0, 10, 2))\n",
    "ls"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "那么你肯定会问这个例子有点。。。弱智 其实不然，与for语句结合还可以创建更加复杂的列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 1, 4, 27, 256, 3125, 46656, 823543, 16777216, 387420489]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ls = [i**i for i in range(10)]\n",
    "ls"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 4.2.2 对数字列表进行简单统计"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "original list: [1, 1, 4, 27, 256, 3125, 46656, 823543, 16777216, 387420489]\n"
     ]
    }
   ],
   "source": [
    "print('original list: ' + str(ls))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the max num of the list387420489\n"
     ]
    }
   ],
   "source": [
    "print('the max num of the list: ' + str(max(ls)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the min num of the list: 1\n"
     ]
    }
   ],
   "source": [
    "print('the min num of the list: ' + str(min(ls)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the sum of the list: 405071318\n"
     ]
    }
   ],
   "source": [
    "print(\"the sum of the list: \" + str(sum(ls)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "当然不止这些，但是这都是最常见的函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "程序运行时间是0.04983115196228027s\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "start = time.time()\n",
    "ls = list(range(0, 1000000))\n",
    "sum(ls)\n",
    "end = time.time()\n",
    "print('程序运行时间是' + str(end - start) + 's')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.3 使用列表的一部分"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 4.3.1 切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['charles', 'martina', 'michael']"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "players = ['charles', 'martina', 'michael', 'florence', 'eli']\n",
    "players[0:3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "就是0到3的元素形成一个新的列表。若没有指定表头，默认从0开始。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['charles', 'martina', 'michael']"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "players[:3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "同样的，没有指定表尾，默认是end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['michael', 'florence', 'eli']"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "players[2:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "也许，你也看出来了这个和索引操作有这异曲同工之妙。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['michael', 'florence', 'eli']"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "players[-3:]  # 打印后3个元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 4.3.2 复制切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "ps = players  # 这样可以吗"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "若我们这是把players的地址复制给了ps, 当我们更改ps中的元素时，players也会跟着变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the list named players : ['charles', 'martina', 'michael', 'florence', 'eli', 'jack', 'jack']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'jack'"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ps.append('jack')\n",
    "print('the list named players : ' + str(players))\n",
    "ps.pop()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "那有什方法可以让他们独立呢。这是我们可以利用切片操作，使得他们独立"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the original ps: ['charles', 'martina', 'michael', 'florence', 'eli', 'jack']\n",
      "now ps: ['charles', 'martina', 'michael', 'florence', 'eli', 'jack', 'david']\n",
      "noe players: ['charles', 'martina', 'michael', 'florence', 'eli', 'jack']\n"
     ]
    }
   ],
   "source": [
    "ps = players[:]\n",
    "print('the original ps: ' + str(ps))\n",
    "ps.append('david')\n",
    "print('now ps: ' + str(ps))\n",
    "print('noe players: ' + str(players))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.4 元组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "不可变的列表称为元组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "元组的一系列操作于列表无异，除了不能更改元组元素之外。因此不再赘述。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.5 小结 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在本章中，你学习了：\n",
    "1. 如何高效地处理列表中的元素；\n",
    "2. 如何使用for 循环遍历列表，Python如何根据缩进来确定程序的结构以及如何避免一些常见的缩进错误；\n",
    "3. 如何创建简单的数字列表，以及可对数字列表执行的一些操作；\n",
    "4. 如何通过切片来使用列表的一部分和复制列表。\n",
    "5. 你还学习了元组（它对不应变化的值提供了一定程度的保护）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
